package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.OnlineClasses;
import com.company.project.service.OnlineClassesService;
import com.company.project.util.FileUploadUtil;
import com.company.project.util.PageUtil;
import com.company.project.util.ReadThVideoLength;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.List;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/online/classes")
public class OnlineClassesController {
    @Resource
    private OnlineClassesService onlineClassesService;

    @Value("${file.videouploadFolder}")
    private String videoPath;

    @Resource
    private FileUploadUtil fileUploadUtil;

    @Resource
    private ReadThVideoLength readThVideoLength;

    @PostMapping("/add")
    public Result add(OnlineClasses onlineClasses) {
        onlineClassesService.save(onlineClasses);
        return ResultGenerator.genSuccessResult();
    }
    @GetMapping("/OCParticular/{id}")
    @ResponseBody
    public  Result selectOCParticular(@PathVariable("id")Integer id){
        List<OnlineClasses> onlineClasses = onlineClassesService.selectOCParticular(id);
        return ResultGenerator.genSuccessResult(onlineClasses);
    }
    /**
     * 删除线上课程
     * @return
     */
    @PostMapping("/deleteOC")
    @ResponseBody
    public Result deleteOC(@RequestBody OnlineClasses onlineClasses) {
        String uri = onlineClasses.getUri();
        File file = new File(videoPath+uri.substring(uri.lastIndexOf("/")+1,uri.length()));
        if (file.exists()){
            file.delete();
        }
        Boolean aBoolean = onlineClassesService.deleteOC(onlineClasses.getId());
        return ResultGenerator.genSuccessResult(aBoolean);
    }

    /**
     * 添加线上课程
     * @param onlineClasses
     * @return
     */
    @PostMapping("/addOnlineClasses")
    @ResponseBody
    public Result addOnlineClasses(@RequestParam String onlineClasses, @RequestParam(required = false) MultipartFile file,@RequestParam(required = false) MultipartFile file1){
        OnlineClasses onlineClasses1 = JSON.parseObject(onlineClasses, OnlineClasses.class);
        String s = "";
        if (file!=null){
            if(file.getOriginalFilename().contains(".pdf")){
                s = fileUploadUtil.writeFile(file, "video");
                onlineClasses1.setType("pdf");
            }else {
                s = fileUploadUtil.writeFile(file, "video");
                String s1 = readThVideoLength.readVideo(videoPath+s.substring(s.lastIndexOf("/")+1,s.length()));
                onlineClasses1.setPlayTime(s1);
                onlineClasses1.setType("video");
            }
            if (file1!=null){
                String s1 = fileUploadUtil.writeFile(file1, "video");
                onlineClasses1.setPhotoUri(s1);
            }
            if (s == null){
                return ResultGenerator.getInternalServerErrot("视频上传错误");
            }
            onlineClasses1.setUri(s);
            Boolean aBoolean = onlineClassesService.addOnlineClasses(onlineClasses1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(s);
            }
        }else {
            Boolean aBoolean = onlineClassesService.addOnlineClasses(onlineClasses1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult("");
            }

        }
        return ResultGenerator.genSuccessResult("传输错误");
    }

    /**
     * 添加上架
     * @param onlineClasses
     * @return
     */
    @PostMapping("/addTopOnlineClasses")
    @ResponseBody
    public Result addTopOnlineClasses(@RequestParam String onlineClasses, @RequestParam(required = false) MultipartFile file,@RequestParam(required = false) MultipartFile file1){
        OnlineClasses onlineClasses1 = JSON.parseObject(onlineClasses, OnlineClasses.class);
        String s = "";
        if (file!=null){
            if(file.getOriginalFilename().contains(".pdf")){
                s = fileUploadUtil.writeFile(file, "video");
                onlineClasses1.setType("pdf");
            }else {
                s = fileUploadUtil.writeFile(file, "video");
                String s1 = readThVideoLength.readVideo(videoPath+s.substring(s.lastIndexOf("/")+1,s.length()));
                onlineClasses1.setPlayTime(s1);
                onlineClasses1.setType("video");
            }
            if (file1!=null){
                String s1 = fileUploadUtil.writeFile(file1, "video");
                onlineClasses1.setPhotoUri(s1);
            }
            if (s == null){
                return ResultGenerator.getInternalServerErrot("视频上传错误");
            }
            onlineClasses1.setUri(s);
            Boolean aBoolean = onlineClassesService.addTopOnlineClasses(onlineClasses1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(s);
            }
        }else {
            Boolean aBoolean = onlineClassesService.addTopOnlineClasses(onlineClasses1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult("");
            }

        }
        return ResultGenerator.genSuccessResult("传输错误");
    }
    /**
     * 修改线上课程
     * @param onlineClasses
     * @return
     */

    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestParam String onlineClasses, @RequestParam(required = false) MultipartFile file,@RequestParam(required = false) MultipartFile file1) {
        OnlineClasses onlineClasses1 = JSON.parseObject(onlineClasses, OnlineClasses.class);
        String s = "";
       if (file!=null){
           if(file.getOriginalFilename().contains(".pdf")){
               s = fileUploadUtil.writeFile(file, "video");
               onlineClasses1.setType("pdf");
           }else {
               s = fileUploadUtil.writeFile(file, "video");
               String s1 = readThVideoLength.readVideo(videoPath+s.substring(s.lastIndexOf("/")+1,s.length()));
               onlineClasses1.setPlayTime(s1);
               onlineClasses1.setType("video");
           }
           if (file1!=null){
               String s1 = fileUploadUtil.writeFile(file1, "video");
               onlineClasses1.setPhotoUri(s1);
           }
           if (s == null){
                return ResultGenerator.getInternalServerErrot("视频上传错误");
            }
           onlineClasses1.setUri(s);
            Boolean aBoolean = onlineClassesService.updateOnlineClasses(onlineClasses1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(s);
            }
        }else {
           Boolean aBoolean = onlineClassesService.updateOnlineClasses(onlineClasses1);
           if (aBoolean.equals(true)){
               return ResultGenerator.genSuccessResult("");
           }
       }

        return ResultGenerator.getInternalServerErrot("传输错误");
    }

    /**
     * 修改上线
     * @param onlineClasses
     * @return
     */
    @PostMapping("/updateTop")
    @ResponseBody
    public Result updateTop (@RequestParam String onlineClasses, @RequestParam(required = false) MultipartFile file,@RequestParam(required = false) MultipartFile file1){
        OnlineClasses onlineClasses1 = JSON.parseObject(onlineClasses, OnlineClasses.class);
        String s = "";
        if (file!=null){
            if(file.getOriginalFilename().contains(".pdf")){
                s = fileUploadUtil.writeFile(file, "video");
                onlineClasses1.setType("pdf");
            }else {
                s = fileUploadUtil.writeFile(file, "video");
                String s1 = readThVideoLength.readVideo(videoPath+s.substring(s.lastIndexOf("/")+1,s.length()));
                onlineClasses1.setPlayTime(s1);
                onlineClasses1.setType("video");
            }
            if (file1!=null){
                String s1 = fileUploadUtil.writeFile(file1, "video");
                onlineClasses1.setPhotoUri(s1);
            }
            if (s == null){
                return ResultGenerator.getInternalServerErrot("视频上传错误");
            }
            onlineClasses1.setUri(s);
            Boolean aBoolean = onlineClassesService.updateTopOnlineClasses(onlineClasses1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(s);
            }
        }else {
            Boolean aBoolean = onlineClassesService.updateTopOnlineClasses(onlineClasses1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult("");
            }
        }
        return ResultGenerator.genSuccessResult("传输错误");
    }
    /**
     * 查询线上课程详情
     * @param id
     * @return
     */
    @GetMapping("/selectOCDetails/{id}")
    @ResponseBody
    public Result selectOnlineClassesById(@PathVariable(value = "id") Integer id){
        OnlineClasses onlineClasses = onlineClassesService.selectOnlineClassesById(id);
        return ResultGenerator.genSuccessResult(onlineClasses);
    }

    /**
     * 查询课程
     * @return
     */
    @GetMapping("/adminSelect/{index}")
    @ResponseBody
    public PageUtil adminSelect(@PathVariable(value = "index" ) Integer index){
        PageUtil<OnlineClasses> onlineClassesPageUtil = onlineClassesService.adminSelect(index);
        return onlineClassesPageUtil;
    }
    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        OnlineClasses onlineClasses = onlineClassesService.findById(id);
        return ResultGenerator.genSuccessResult(onlineClasses);
    }

    //获取课程
    @GetMapping("/list")
    public Result list() {
        List<OnlineClasses> list = onlineClassesService.getOnlineClasses();
        for(int i=0;i<list.size();i++){
            if(list.get(i).getCreateTimeFormate()!=null){
                list.get(i).setCreateTimeFormate(list.get(i).getCreateTimeFormate().substring(5));
            }
        }
        return ResultGenerator.genSuccessResult(list);
    }

    //获取全部课程，分页
    @GetMapping("/list/all/{index}")
    public Result listAll(@PathVariable("index") Integer index) {
        PageUtil<OnlineClasses> list = onlineClassesService.getOnlineClassesAll(index);
        return ResultGenerator.genSuccessResult(list);
    }

}
